﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using Yugi.Client.WebSerive;

namespace WpfApplication1
{
	/// <summary>
	/// Interaction logic for ListTable.xaml
	/// </summary>
	public partial class ListTable : UserControl
	{
        private DispatcherTimer on_out;
		public ListTable()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(ListTable_Loaded);
		}

        void LoadData()
        {
            //int d = 10;
            //for (int i = 0; i < 1; i++)
            //{
            //    Table table = new Table();
            //    Canvas.SetTop(table, 11);
            //    Canvas.SetLeft(table, 8);
            //    table.Margin = new Thickness((i) * (d + 130), 0, 0, 0);
            //    table.DataContext = null;
            //    layout.Children.Add(table);
            //    table.OnClick += new Action<object>(table_OnClick);
            //}
        }
        public void AddTable(TableInfo obj)
        {
            int d = 10;
            int i = this.layout.Children.Count;
            Table table = new Table();
            table.ID = (int)obj.TableID;
            table.Player1 = obj.NickName1;
            table.Player2 = obj.NickName2;
            table.DataContext = obj;
            Canvas.SetTop(table, 11);
            Canvas.SetLeft(table, 8);
            table.Margin = new Thickness((i) * (d + 130), 0, 0, 0);
            layout.Children.Add(table);
            table.OnClick += new Action<object>(table_OnClick);
        }
        public void RemoveTable(int tableid)
        {
            foreach (Table i in this.layout.Children)
            {
                if (i.ID == tableid)
                {
                    layout.Children.Remove(i);
                    return;
                }
            }
        }
        public void Delete(int index_begin)
        {
            this.layout.Children.RemoveRange(index_begin, this.layout.Children.Count - 1);
        }
        void table_OnClick(object obj)
        {
            Table tb = obj as Table;
            if (tb == null) return;
            if (OnChoice != null)
                OnChoice(tb.DataContext);
        }
        void LoadNextData()
        {
            int d = 10;
            for (int i = 0; i < 4; i++)
            {
                Table table = new Table();
                Canvas.SetTop(table, 11);
                Canvas.SetLeft(table, 8);
                table.Margin = new Thickness((i) * (d + 130), 0, 0, 0);
                layout.Children.Add(table);
                table.OnClick += new Action<object>(table_OnClick);
            }
        }
        void ClearData()
        {
            layout.Children.RemoveRange(0, 4);
        }
        void ListTable_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
            on_out = new DispatcherTimer();
            on_out.Interval = TimeSpan.FromMilliseconds(15);
            on_out.Tick += new EventHandler(on_out_Tick);
            on_out.Stop();
        }

        double dx=0;
        DateTime run_out;
        void on_out_Tick(object sender, EventArgs e)
        {

            for (int i = 0; i < layout.Children.Count; i++)
            {
                Table table = layout.Children[i] as Table;
                table.Margin = new Thickness(table.Margin.Left + dx, 0, 0, 0);
            }
            dx += 0.5;
            DateTime temp = DateTime.Now;
            if ((temp - run_out).TotalMilliseconds > 800)
            {
                on_out.Stop();
                ClearData();
                LoadNextData();
            }
        }
        public void Begin()
        {
            dx = 0;
            run_out = DateTime.Now;
            on_out.Start();
        }
        public event Action<object> OnChoice;

        internal Table GetTableRandom()
        {
            foreach (Table tmp in this.layout.Children)
            {
                if (((TableInfo)tmp.DataContext).PlayerID1 != -1)
                {
                    return tmp;
                }
            }
            return null;
        }
    }
}